package com.glq1218.auth.controller;

import com.glq1218.auth.bo.AuthBO;
import com.glq1218.auth.bo.LoginBO;
import com.glq1218.auth.bo.RegisterBO;
import com.glq1218.auth.dto.AuthDTO;
import com.glq1218.auth.dto.LoginDTO;
import com.glq1218.auth.dto.RegisterDTO;
import com.glq1218.auth.service.AuthenticationService;
import com.glq1218.domain.R;
import com.glq1218.util.BeanCopyUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author glq1218
 */
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {
    private final AuthenticationService authenticationService;

    @PostMapping("/login")
    @Operation(summary = "登录", description = "认证用户")
    public R<AuthDTO> login(@RequestBody LoginDTO loginDTO) {
        LoginBO loginBO = BeanCopyUtils.copyBean(loginDTO, LoginBO.class);
        AuthBO authBO = authenticationService.authenticate(loginBO);
        AuthDTO authDTO = BeanCopyUtils.copyBean(authBO, AuthDTO.class);
        return R.ok(authDTO);
    }

    @PostMapping("/forget")
    public R<?> forget(@RequestBody Map<String, String> params) {
        return R.ok(authenticationService.forget(params.get("username"), params.get("phone"), params.get("password")));
    }

    @PostMapping("/updatePassword")
    public R<?> updatePassword(@RequestBody Map<String, String> params) {
        return R.ok(authenticationService.updatePassword(params.get("oldPassword"), params.get("newPassword")));
    }

    @PostMapping("/register")
    public R<?> register(@RequestBody RegisterDTO registerDTO) {
        RegisterBO registerBO = BeanCopyUtils.copyBean(registerDTO, RegisterBO.class);
        return R.ok(authenticationService.register(registerBO));
    }
}
